package com.bs.web.filter;

import com.bs.vo.RestResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 说明：
 * 自定义错误页面
 *
 * @author 陈晓琪dellNotebook
 * @since 2025年05月11日 星期日 12:56:09
 */
@RestController
@Slf4j
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public ResponseEntity<RestResultVo<String>> handleError(HttpServletRequest request, HttpServletResponse response) {
        String uri = request.getRequestURI();
        String method = request.getMethod();
        HttpStatus status = HttpStatus.valueOf(response.getStatus());

        // 返回 JSON 格式的 404 错误
        ResponseEntity<RestResultVo<String>> responseEntity = new ResponseEntity<>(RestResultVo.failed("页面不存在"), status);
        log.info("[{}][{}] 请求失败: {}", method, uri, responseEntity);
        return responseEntity;
    }

    @Override
    public String getErrorPath() {
        return null;
    }
}